package toanstt.gameplay;



import toanstt.com.T;
import toanstt.com.Vec2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;

public class CellTable 
{
	//int ICELL = 11;
	static public Cell[] cell;
	static int LENG_ARRAY = 121;
	
	public static int[] cell_index_init;
	public static int[][] cell_index;
	public static int need_update_position=5;
	public CellTable()
	{
		cell_index_init = new int[T.COLS];
		for(int i =0;i<T.COLS;i++)
			cell_index_init[i] = -1;
		
		cell_index = new int[T.COLS][];
		for(int i =0;i<T.COLS;i++)
			cell_index[i] = new int[T.ROWS];
		T.numellcaled = 0;
		
		
		for(int i =0;i<T.COLS;i++)
			for(int j =0;j<T.ROWS;j++)
			{
				if(T.lockedcells[i][j] == 1)
					cell_index[i][j] = -2;
				else
				{
					cell_index[i][j] = -1;
					T.numellcaled++;
				}
			}
		
		
		cell = new Cell[LENG_ARRAY];
		//AddCell(0);
		//cell_index[0][0] = 0;
	}
	
	public void AddCell(int bin)
	{
		int index =FinIndexNullCell(); 
		if(index == -1)
			return;
		cell[index] = new Cell(bin,index);
		//Log.d("AddCell",""+index);
	}
	
	public void DestroyCell(int c,int r)
	{
		
	}
	public void ReSetCellTable()
	{
		
		
		
	}
	public void Draw(Canvas canvas)
	{
		DrawLockedCells(canvas);
		for(int i=0;i<LENG_ARRAY;i++)	
			//for(int j = T.ROWS-1;j>=0;j--)
				//if(cell[i][j]!=null)
					//cell[i][j].Update();
			if(cell[i]!=null)
				cell[i].Draw(canvas);
	}
	public void Update()
	{
		
		UpdateCellINdex();
		if(need_update_position>0)
		{
		//Log.d("UPloadding","true");
		need_update_position--;
		for(int i=LENG_ARRAY-1;i>=0;i--)	
			if(cell[i]!=null)
				cell[i].Update2();
		GameManagement.timeline.ispause = true;
		}
		else
			GameManagement.timeline.ispause = false;
		//UpdateAddWithNullInit();
		UpdateAddWithNullInit2();
	}
	static public Vec2 Getcellwithcoor(int x,int y)
	{
		return new Vec2((x-T.GAMESCREEN_MIN_X)/ T.CELL_WIDTH,(y-T.GAMESCREEN_MIN_Y)/ T.CELL_HEIGHT);
	}
	static public int GetINdexcellwithcoor(int x,int y)
	{
		//Log.d("cell_index",((x-T.GAMESCREEN_MIN_X)/ T.CELL_WIDTH)+" "+((y-T.GAMESCREEN_MIN_Y)/ T.CELL_HEIGHT));
		if((y-T.GAMESCREEN_MIN_Y)/ T.CELL_HEIGHT>=T.ROWS)
			return -1;
		if(x-T.GAMESCREEN_MIN_X <0)
			return -1;
		if(y-T.GAMESCREEN_MIN_Y <0)
			return -1;
		
		return cell_index[(x-T.GAMESCREEN_MIN_X)/ T.CELL_WIDTH][(y-T.GAMESCREEN_MIN_Y)/ T.CELL_HEIGHT];
	}
	public static int GetROWwithcoor(int y)
	{
		return (y-T.GAMESCREEN_MIN_Y)/ T.CELL_HEIGHT;
	}
	public int FinIndexNullCell()
	{
		for(int i = 0;i<LENG_ARRAY;i++)
		{
			if(cell[i]==null)
				return i;
		}
		return -1;
	}
	public void UpdateCellINdex()
	{
		if(SelectedCell.index_done_draw != SelectedCell.n2)
		{
			need_update_position =0;
			return;
		}
		int dem =0;
		for(int i =0;i<T.COLS;i++)
		{
			cell_index_init[i] = -1;
			for(int j =0;j<T.ROWS;j++)
				if(cell_index[i][j]!=-2)
					cell_index[i][j] = -1;
		}
		Vec2 temp;
		//need_update_position = false;
		for(int i=0;i< LENG_ARRAY ;i++)
			if(cell[i]!=null)
			{
				
				temp = Getcellwithcoor(cell[i].x,cell[i].y);
				
				
				if(cell[i] == null)
					continue;
				if(cell[i].y<0)
				{
					
					if(temp.x < 0 || temp.x >= 11)
						continue;
					cell_index_init[temp.x] =cell[i].index; 
					
				}
				else
				{
					if(temp.x>=11 ||temp.y>=10 )
						continue;
					cell_index[temp.x][temp.y] = cell[i].index;
					
				}
				dem++;
			}
		if(dem <T.numellcaled + 11)
				need_update_position = 4;
		
	}
	public void UpdateAddWithNullInit2()
	{

		for(int i =0;i<T.COLS;i++)
		{
			
			if(cell_index_init[i]==-1)
			{
				AddCell(i);
			}
			else if(cell[cell_index_init[i]].y > 0)
			{
				AddCell(i);
			}
		}
	}
	public void UpdateAddWithNullInit()
	{

		for(int i =0;i<T.COLS;i++)
		{
			
			if(cell_index_init[i]==-1)
			{
				//if(cell_index[i][0]==-1)
					//need_update_position = true;
				AddCell(i);
				
			}
		}
	}
	public void DeleteCell(int x,int y,int ups)
	{
		int index = cell_index[x][y];
		if(index >=0 && index < LENG_ARRAY)
		{
			cell[index] = null;
		
			if(ups>0)
			{
				for(int i=y-1;i>=0;i--)
				{
					index = cell_index[x][i];
					cell[index].v_y = -ups; 
				}
			}
		}
	}
	static public void DeleteCellAxis(int x,int y,int ups)
	{
		Vec2 temp = Getcellwithcoor(x,y);
		int index = cell_index[temp.x][temp.y];
		if(index >=0 && index < LENG_ARRAY)
		{
			cell[index] = null;
		
			if(ups>0)
			{
				for(int i=temp.y-1;i>=0;i--)
				{
					index = cell_index[temp.x][i];
					cell[index].v_y = -ups; 
				}
			}
		}
			
	}
	static public void DeleteCellAxis2(int x,int y,int ups)
	{
		//Vec2 temp = Getcellwithcoor(x,y);
		int index = cell_index[x][y];
		
		if(index >=0 && index < LENG_ARRAY)
		{
			cell[index] = null;
		
			if(ups>0)
			{
				for(int i=y-1;i>=0;i--)
				{
					index = cell_index[x][i];
					if(index >=0 && index < LENG_ARRAY)
						if(cell[index]!=null)
							cell[index].v_y = -ups; 
				}
			}
		}
			
	}
	public void DrawLockedCells(Canvas canvas)
	{
		
		for(int i =0;i<T.COLS;i++)
			for(int j =0;j<T.ROWS;j++)
				if(cell_index[i][j]==-2)
				{
					T.mat = new Matrix();
					T.mat.postScale(T.CELL_WIDTH*1.0f/60, T.CELL_WIDTH*1.0f/60);
					T.mat.postTranslate(T.GAMESCREEN_MIN_X + i*T.CELL_WIDTH,  T.GAMESCREEN_MIN_Y + j*T.CELL_HEIGHT);
					
					canvas.drawBitmap(T.bitmap_locked, T.mat, null);
				}
	}
}
